<template>
  <div>
    <el-dialog
      title="修改预约"
      :visible.sync="dialogVisible"
      width="750px"
      :close-on-click-modal="false"
      append-to-body
      @close="close"
    >
      <el-form
        ref="form"
        v-loading="loading"
        :model="form"
        :rules="rules"
        size="small"
        label-width="100px"
      >
        <el-form-item label="订单编号：" prop="orderNo">
          <el-input v-model="form.orderNo" :disabled="true" maxlength="100" />
        </el-form-item>
        <el-form-item label="预约时间：" prop="time">
          <el-date-picker
            v-model="form.newDate"
            :default-time="['00:00:00', '23:59:59']"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间"
            align="right"
            :picker-options="pickerOptions"
          />
        </el-form-item></el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" round :disabled="loading" @click="hide">取 消</el-button>
        <el-button
          size="small"
          round
          :disabled="loading"
          type="primary"
          @click="submit"
        >修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { amend } from '@/api/reservation'
export default {
  data() {
    return {
      // 校验
      rules: {
        remark: [{ required: true, message: '请输入备注', trigger: 'change' }]
      },
      // 配置
      resolve: null,
      reject: null,
      // 状态
      loading: false,
      dialogVisible: null,
      // 数据
      detail: '',
      form: {
        orderNo: null,
        newDate: null
      },
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 86400000
        }
      }
    }
  },
  watch: {
    'form.newDate': {
      handler(v) {
        v && this.$set(this.form, 'newDate', v)
      },
      deep: true
    }
  },
  methods: {
    /**
     * 弹框相关
     */
    open({ detail }) {
      this.detail = JSON.parse(JSON.stringify(detail))
      this.form.orderNo = detail.orderNo
      this.$set(this.form, 'newDate', detail.time)
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    close() {
      this.detail = ''
      this.form = {
        orderNo: null,
        remark: null
      }
      this.$refs.form.resetFields()
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    /**
     * 数据组
     */

    /**
     * 按钮组
     */
    // 确 定
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true
          amend(this.form)
            .then((res) => {
              this.$notify({
                title: '成功',
                message: '修改成功',
                type: 'success',
                duration: 2000,
                offset: 40
              })
              this.resolve && this.resolve()
              this.hide()
            })
            .finally(() => {
              this.loading = false
            })
        }
      })
    }

    /**
     * 状态组
     */

    /**
     * 方法组
     */
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped></style>
